//controlls
define(function (require, exports, module) {
    const Vue=require("vue");
    var template=require('text!./index.html');
    const api=require("api");
    const weui=require('weui');
    module.exports=Vue.extend({
        template:template,
        data:function () {
            return {
                useBikeData:{},
                timerId:0
            }
        },
        computed:{
            timer:function () {
                try {
                    var old = new Date(this.$route.params.time).getTime();
                    var now = new Date().getTime();
                    var t=now-old;
                    return parseInt(t/1000);
                }catch(e){
                    return 0;
                }

            },
            isBack:function () {
                return this.$store.getters.getIsBack;
            },
            user_info:function () {
                return this.$store.getters.getUserInfo;
            }
        },
        beforeRouteEnter:function (to,from,next) {
            next(function (vm) {
                if(!vm.isBack){
                    vm.useBikeStatus();
                    vm.timerSet();
                }
            })
        },
        methods: {
            useBikeStatus: function () {
                var v=this;
                var code=v.$route.params.code;
                var time=v.$route.params.time;
                var loading = weui.loading('loading……');
                //查询用车状态
                api.useBikeStatus(code,time,v.user_info).then(function (res) {
                    if(res.user_token_invalid){
                        v.$store.commit("logOut");
                        v.$router.push({path: '/phone'});
                        return;
                    }
                    var data=res.query_user_use_bike_status;
                    if(data&&data.return_code==="SUCCESS"){
                        v.useBikeData=data;
                    }else {
                        weui.topTips(data.return_msg||'网络异常！');
                    }
                }).catch(function (e) {
                    weui.topTips("网络异常!");
                }).finally(function () {
                    loading.hide();
                });
            },
            timerSet:function(){
                var v=this;
                if(v.timerId){clearInterval(v.timerId);}
                v.timerId=setInterval(function () {
                    v.timer++;
                },1000);
            },
            getTimer:function () {
                if(this.timer<=60){
                    return 1;
                }
                return parseInt(this.timer/60)+1;
            }
        }
    });
});
